package cn.iocoder.yudao.module.resource.controller.admin.tag;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.resource.controller.admin.tag.vo.ResourceTagPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.tag.vo.ResourceTagRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.tag.vo.ResourceTagSaveReqVO;
import cn.iocoder.yudao.module.resource.convert.ResourceTagConvert;
import cn.iocoder.yudao.module.resource.dal.dataobject.tag.ResourceTagDO;
import cn.iocoder.yudao.module.resource.service.tag.ResourceTagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 资源���签")
@RestController
@RequestMapping("/resource/tag")
@Validated
public class ResourceTagController {

    @Resource
    private ResourceTagService tagService;

    @PostMapping("/create")
    @Operation(summary = "创建资源标签")
    @PreAuthorize("@ss.hasPermission('resource:tag:create')")
    public CommonResult<Long> createTag(@Valid @RequestBody ResourceTagSaveReqVO createReqVO) {
        return success(tagService.createTag(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资源标签")
    @PreAuthorize("@ss.hasPermission('resource:tag:update')")
    public CommonResult<Boolean> updateTag(@Valid @RequestBody ResourceTagSaveReqVO updateReqVO) {
        tagService.updateTag(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资源标签")
    @Parameter(name = "id", description = "标签ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:tag:delete')")
    public CommonResult<Boolean> deleteTag(@RequestParam("id") Long id) {
        tagService.deleteTag(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Operation(summary = "批量删除资源标签")
    @Parameter(name = "ids", description = "标签ID列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('resource:tag:delete')")
    public CommonResult<Boolean> deleteTagList(@RequestParam("ids") List<Long> ids) {
        tagService.deleteTagList(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资源标签")
    @Parameter(name = "id", description = "标签ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:tag:query')")
    public CommonResult<ResourceTagRespVO> getTag(@RequestParam("id") Long id) {
        ResourceTagDO tag = tagService.getTag(id);
        return success(BeanUtils.toBean(tag, ResourceTagRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资源标签分页")
    @PreAuthorize("@ss.hasPermission('resource:tag:query')")
    public CommonResult<PageResult<ResourceTagRespVO>> getTagPage(@Valid ResourceTagPageReqVO pageReqVO) {
        PageResult<ResourceTagDO> pageResult = tagService.getTagPage(pageReqVO);
        return success(ResourceTagConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资源标签 Excel")
    @PreAuthorize("@ss.hasPermission('resource:tag:export')")
    public void exportTagExcel(@Valid ResourceTagPageReqVO pageReqVO,
                               HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceTagDO> list = tagService.getTagPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资源标签.xls", "数据", ResourceTagRespVO.class,
                BeanUtils.toBean(list, ResourceTagRespVO.class));
    }

}
